import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

// Finding the H, U, T, L 
// TrinhNX 08/12: I used char array instead of boolean
// It is because for simply the reading process. 
// No need to process data
// TODO 2 :  it is better to wrap all two functions into 1 function
// 		Implement at home
// TODO 3 : Find algorithm for better solution, check performance
// 		Idea about the count sum index dot :D 
public class SamsungTest5 {

	private static final int TABLE_SIZE = 6;
	private static final int BLOCK_SIZE = 3;
	private static final int NUM_TEST_CASE = 1;

	private static final int U_BLOCK = 1;
	private static final int H_BLOCK = 2;
	private static final int L_BLOCK = 3;
	private static final int T_BLOCK = 4;

	public static void main(String[] agrs) throws FileNotFoundException {
		SamsungTest5 main = new SamsungTest5();
		for (int test_case = 0; test_case < NUM_TEST_CASE; test_case++) {
			// 1. Initialize the necessary variables
			char[][] source = new char[TABLE_SIZE][TABLE_SIZE];
			int num_H = 0, num_L = 0, num_T = 0, num_U = 0;
			// 2. Read file into source, 1 for true and 0 for false
			Scanner scanner = new Scanner(new File("data\\samsung5.txt"));
			for (int i = 0; i < TABLE_SIZE; i++) {
				source[i] = scanner.next().toCharArray();
			}
			// 3. Loop each block BLOCK_SIZE to get the sub matrix
			for (int x = 0; x < TABLE_SIZE; x = x + BLOCK_SIZE) {
				for (int y = 0; y < TABLE_SIZE; y = y + BLOCK_SIZE) {
					// 4. Here, we had (block_x, block_y) -> top left position of the sub matrix
					// 5. Call the sub function to calculate the dot
					// NOTE: Use switch to avoid duplicated calculation
					switch (main.updateCheckBlock(source, x, y, BLOCK_SIZE)) {
						case H_BLOCK: num_H++; break;
						case U_BLOCK: num_U++; break;
						case L_BLOCK: num_L++; break;
						case T_BLOCK: num_T++; break;
					}
				}
			}
			scanner.close();
			System.out.println("Number of H " + num_H);
			System.out.println("Number of L " + num_L);
			System.out.println("Number of T " + num_T);
			System.out.println("Number of U " + num_U);
		}
	}

	private int updateCheckBlock(final char[][] source, final int x,
			final int y, final int size) {
		// Check the middle one
		// Zero : => L or U
		//	 If there is a same pair and different pair => U, else L
		// (1, 0) (1, 2) and (0, 1) ( 2, 1)
		// Else : => T or H
		// 		If two opposite corner the same => H , else T
		// -1 because array index starts from 0
		final int middle = size / 2;
		if (source[x + middle][y + middle] == '0') {
			if ((source[x][y + middle] == source[x + size - 1][y + middle]) ||
					(source[x + middle][y] == source[x + middle][y + size - 1]))
				return U_BLOCK;
			return L_BLOCK;
		} else {
			if (source[x][y] != source[x + size - 1][y + size - 1])
				return T_BLOCK;
			return H_BLOCK;
		}
	}
}